package edu.gatech.volt2.drdoommgt.presenter;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import edu.gatech.volt2.drdoommgt.domain.*;
import edu.gatech.volt2.drdoommgt.gui.*;
import edu.gatech.volt2.drdoommgt.system.DataBaseAccessor;

public class FindByNamePresenter implements ActionListener{
	
	private static DataBaseAccessor dba;
	private INameView myView;
	
	public FindByNamePresenter(INameView view)
	{
		this.myView= view;
		this.myView.addListener(this);
		this.myView.showSelf();
	}


	public void actionPerformed(ActionEvent event) {
		if ( event.getSource() == myView.getCancelButton()){
			System.out.println("Exiting..");
			myView.hideSelf();
		}
		else if( event.getSource() == myView.getSubmitButton()){
			dba= new DataBaseAccessor();
			dba.connectToDatabase();
			
			Patient patient= null;
			
			JTextField firstField= myView.getFirstNameTextField();
			JTextField middleField= myView.getMiddleNameField();
			JTextField lastField= myView.getLastNameField();
			
			String first= firstField.getText();
			String middle= middleField.getText();
			String last= lastField.getText();
			
			Name nameAttempt= new Name(first, middle, last);
			
			patient= dba.findPatientByName(nameAttempt);
			
			if(patient!=null){
//				JOptionPane.showMessageDialog(null, patient.getName().toString()+"\nID: "+
//						patient.getId()+"\n"+ patient.getEmail()+"\n"+(patient.getInfo().toString()));
				EditUserInfoPresenter pres= new EditUserInfoPresenter(patient, new PatientInformationWindow());

			}	
		}
		else{//do nothing
			
		}
		
	}
}
